package cn.iocoder.yudao.module.expert.service.expertinfo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.expert.controller.admin.expertinfo.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.expertinfo.ExpertInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.expert.dal.mysql.expertinfo.ExpertInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.expert.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 专家信息 Service 实现类
 *
 * @author 周天贺
 */
@Service
@Validated
public class ExpertInfoServiceImpl implements ExpertInfoService {

    @Resource
    private ExpertInfoMapper infoMapper;

    @Override
    public Long createInfo(ExpertInfoSaveReqVO createReqVO) {
        // 插入
        ExpertInfoDO info = BeanUtils.toBean(createReqVO, ExpertInfoDO.class);
        infoMapper.insert(info);
        // 返回
        return info.getId();
    }

    @Override
    public void updateInfo(ExpertInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateInfoExists(updateReqVO.getId());
        // 更新
        ExpertInfoDO updateObj = BeanUtils.toBean(updateReqVO, ExpertInfoDO.class);
        infoMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfo(Long id) {
        // 校验存在
        validateInfoExists(id);
        // 删除
        infoMapper.deleteById(id);
    }

    private void validateInfoExists(Long id) {
        if (infoMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public ExpertInfoDO getInfo(Long id) {
        return infoMapper.selectById(id);
    }

    @Override
    public PageResult<ExpertInfoDO> getInfoPage(ExpertInfoPageReqVO pageReqVO) {
        return infoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ExpertInfoDO> getAllExpertInfo(String expertMajor) {
        return infoMapper.selectList(expertMajor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public boolean importExpertInfoList(List<ExpertInfoImportExcelVO> importExpertInfos) {
        // 1.1 参数校验
        if (CollUtil.isEmpty(importExpertInfos)) {
            throw exception(EXPERT_INFO_IMPORT_LIST_IS_EMPTY);
        }
        List<ExpertInfoDO> importExpertDOs=BeanUtils.toBean(importExpertInfos, ExpertInfoDO.class);
        return infoMapper.insertBatch(importExpertDOs);
    }
}